package com.sec.bnjey;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.sec.bnjey.adapter.BluetoothSelectionAdatper;
import com.sec.bnjey.bluetooth.BtConfig;
import com.sec.bnjey.vo.VOBluetoothSelectionListView;

@SuppressLint("NewApi")
public class IntroActivityA extends Activity {

	private BluetoothAdapter mBTAdapter = null;
	private int REQUEST_ENABLE_BT = 0;
	private ListView mBluetoothSelectionListView = null;
	private BluetoothSelectionAdatper mBluetoothSelectionAdatper = null;
	private Activity mActivity = null;
	private ArrayList<VOBluetoothSelectionListView> mBtData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		Log.i("josung", "IntroActivity start ");
		init();
		bluetoothAdapterCheck();
		bluetoothVitality();
	}

	private void init() {
		mActivity = this;
		IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		this.registerReceiver(mReceiver, filter);
//		mBluetoothSelectionListView = (ListView) findViewById(R.id.bluetooth_listview);
	}

	private void setAdatper(ArrayList<VOBluetoothSelectionListView> btData) {
		mBluetoothSelectionAdatper = new BluetoothSelectionAdatper(mActivity, btData);
		mBluetoothSelectionListView.setAdapter(mBluetoothSelectionAdatper);
	}

	private void bluetoothAdapterCheck() {
		// ������� ��͸� ��
		mBTAdapter = BluetoothAdapter.getDefaultAdapter();
		if (mBTAdapter == null) {
			Toast.makeText(this, "������� ����� �� ����ϴ�.", Toast.LENGTH_SHORT).show();
			finish();
		}
	}

	private void bluetoothVitality() {
		// ��������� �������� ������ ������� ���� �˾� ��(os)
		if (!mBTAdapter.isEnabled()) {
			Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		} else {
			// ���� ��� ��ȸ
			if (!bluetoothDevicesFairing()) {
				if (mBTAdapter.isDiscovering()) {
					mBTAdapter.cancelDiscovery();
				}
				mBTAdapter.startDiscovery();
			}
		}
	}

	private boolean bluetoothDevicesFairing() {
		Set<BluetoothDevice> pairedDevices = mBTAdapter.getBondedDevices();
		if (pairedDevices.size() > 0) {
			for (BluetoothDevice device : pairedDevices) {
				Log.i("josung", "Fairing getAddress = " + device.getAddress());
				Log.i("josung", "Fairing getName = " + device.getName());
				if (device.getName().equals(BtConfig.OBD_DEVICE_NAME)) {
					// bluetoothDevicesConnection(device.getAddress());
				}
			}
			Log.i("josung", "ok Fairing");
			return true;
		}
		Log.i("josung", "not Fairing");
		return false;

	}

	private void bluetoothDevicesConnection(String address) {
		Log.i("josung", "bluetoothDevicesConnection = " + address);
		mBTAdapter.cancelDiscovery();
		try {
			BluetoothDevice device = mBTAdapter.getRemoteDevice(address);
			BluetoothSocket mSocket = device.createInsecureRfcommSocketToServiceRecord(BtConfig.OBD_DEVICE_UUID);
			mSocket.connect();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	final BroadcastReceiver mReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			Log.i("josung", "onReceive");
			String action = intent.getAction();
			if (BluetoothDevice.ACTION_FOUND.equals(action)) {
				BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				Log.i("josung", "onReceive getName = " + device.getName());
				Log.i("josung", "onReceive getAddress = " + device.getAddress());
				if (device.getName().equals(BtConfig.OBD_DEVICE_NAME)) {
					// bluetoothDevicesConnection(device.getAddress());
				}
			}
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case 0:
				Toast.makeText(this, "������� Ȱ��ȭ", Toast.LENGTH_SHORT).show();
				bluetoothVitality();
				break;
			}
		} else {
			Toast.makeText(this, "������� ��Ȱ��ȭ", Toast.LENGTH_SHORT).show();
			finish();
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		if (mBTAdapter != null) {
			mBTAdapter.cancelDiscovery();
		}
	}
}
